<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :collapse="collapse"
      background-color="#324157"
      text-color="#bfcbd9"
      active-text-color="#20a0ff"
      unique-opened
      router
    >
      <template v-for="item in items">
        <template v-if="item.children && item.children.length != 0">
          <el-submenu :index="item.url" :key="item.url">
            <template slot="title">
              <i :class="item.icon"></i>
              <span slot="title">{{ item.name }}</span>
            </template>
            <template v-for="subItem in item.children">
              <el-submenu v-if="false" :index="subItem.url" :key="subItem.url">
                <template slot="title">{{ subItem.name }}</template>
              </el-submenu>
              <el-menu-item v-else :index="subItem.url" :key="subItem.url">{{
                subItem.name
              }}</el-menu-item>
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.url" :key="item.url">
            <i :class="item.icon"></i>
            <span slot="title">{{ item.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script>
import bus from "./bus";
import { mapMutations } from "vuex";
// import { res } from "./databases/Sidebar";
export default {
  data() {
    return {
      collapse: false,
      items: [],
    };
  },
  watch: {
    $route: function(newPath, oldPath) {
      console.log(newPath, oldPath);
      if (newPath.path == "/config") {
        this.init();
      }
    },
  },
  computed: {},
  created() {
    bus.$on("collapse", (msg) => {
      this.collapse = msg;
      bus.$emit("collapse-content", msg);
    });
    this.init();
  },
  methods: {
    init() {
      this.getSidebar();
      this.getPermissions();
    },
    getPermissions() {
      this.$axios.get("/sys/menu/permissions", {}).then(({ data: res }) => {
        // console.log(res);
        if (res.code != 0) {
          return;
        }
        this.setPermissions(res.data);
      });
    },
    ...mapMutations({
      setPermissions: "SET_PERMISSIONS",
    }),
    getSidebar() {
      this.$axios
        .get("/sys/menu/nav", { params: { _t: Date.now() } })
        .then(({ data: res }) => {
          console.log("侧边栏数据", res);
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }

          this.items = res.data;
          this.items.map((item, index) => {
            item.url = !item.url ? index.toString() : item.url;
          });
        });

      // this.items = res.data;
      // this.items.map((item, index) => {
      //   item.url = !item.url ? index.toString() : item.url;
      // });
    },
  },
};
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.sidebar > ul {
  height: 100%;
}
</style>
